//
//  RJCountryConnectView.m
//  AirQuality
//
//  Created by Po on 15/3/11.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJCountryConnectView.h"
#import "RJCountryConnectModel.h"
#import "RJLoadView.h"
#import "XYViewController.h"
#import "RJDounghnutView.h"


@interface RJCountryConnectView () <RJHomeBottomFatherViewDelegate>

- (void)initData;
- (void)initInterface;
- (void)initNet;
@property (retain, nonatomic) RJDounghnutView * dounghnutView;      //环状图
@end

@implementation RJCountryConnectView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initData];
        [self initInterface];
        [self initNet];
    }
    return self;
}

#pragma mark - user-defined initialization function

- (void)initData
{
    self.type = HomeBottomTablePosition;
    [self setDelegate:self];
}

- (void)initInterface
{
    UIImageView * pieBackImage = [[UIImageView alloc] initWithFrame:self.pieContentView.bounds];
    [pieBackImage setImage:GetImage(@"connectContryBack.png")];
    [self.pieContentView addSubview:pieBackImage];
    self.pieContentView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.pieContentView.layer.shadowOffset = CGSizeMake(0,1);
    self.pieContentView.layer.shadowOpacity = 0.6;
    self.pieContentView.layer.shadowRadius = 1.0;
}

- (void)initNet
{
    self.isConnecting = YES;
    [RJNetWork myNetRequestWithAddress:[NetURL defaultURL].CountryConnect data:@{} completeBlock:^(id responseObject) {
        if ([RJUserData defaultRJUserData].isProvince) {
            self.offLine = [[responseObject objectForKey:@"ALLOFFLINE"] integerValue];
            self.onLine = [[responseObject objectForKey:@"ALLONLINE"] integerValue];
            self.detailData = [NSArray arrayWithArray:[responseObject objectForKey:@"DATA"]];
        } else {
            self.offLine = 0;
            self.onLine = 0;
            NSMutableArray * offLineStations = [[NSMutableArray alloc] init];
            for (NSDictionary * dic in responseObject) {
                if ([dic[@"STATUS"] isEqualToString:@"在线"]) {
                    self.onLine += 1;
                } else {
                    self.offLine += 1;
                    [offLineStations addObject:dic[@"STATIONNAME"]];
                }
            }
            self.detailData = [NSArray arrayWithArray:offLineStations];
        }
        
        [self netFinish:NetRequestStatusSucceed];
        [self createPieView];
        [self freeLoadingView];
    }failBlack:^(id response) {
        [self netFinish:NetRequestStatusFailure];
        [RJTools showTitle:@"网络异常" msg:@"网络异常，请点击重试！" cancelString:@"取消" confirmString:@"重试" confirmBlock:^{
            [self initNet];
        } vc:self.vc];
    }];
}

- (void)createPieView
{
    
    [self.pieContentView addSubview:[self getPieView:self.pieContentView.bounds title:@"农村实时联网"]];
    [self.pieContentView bringSubviewToFront:self.pieTitleLabel];
    [self.pieView setValueArr:@[@(self.onLine),@(self.offLine)]];
    [self.pieView setTitleArr:@[[NSString stringWithFormat:@"在线%@个",@(self.onLine)],[NSString stringWithFormat:@"离线%@个",@(self.offLine)]]];
    CGFloat width = self.frame.size.width;
    [self.pieView setTitleRect:CGRectMake(width / 2, 15, width / 2, self.bounds.size.height - 30)];
    [self.pieView setColorArr:@[[UIColor greenColor],GetRGBColor(200, 200, 200, 1)]];
    self.pieView.scaleY = 0;
    self.pieView.spaceHeight = 0;
    [self showPieViewButton];
    //设置圆环
    NSArray *dataArray = @[[NSNumber numberWithInt:self.onLine],[NSNumber numberWithInt:self.offLine]];
    NSArray *colorsArray = [NSArray arrayWithObjects:
                            [UIColor greenColor],
                            [UIColor colorWithRed:188/255.0 green:188/255.0  blue:188/255.0  alpha:1],
                            nil];
    CGRect rect;
    if (DeviceType < 5) {
        rect.size.width = self.pieView.bounds.size.width/2 - 10;
        rect.size.height = self.pieView.bounds.size.height/2 - 10;
    }else if (DeviceType >= 5 && DeviceType < 6){
        rect.size.width = self.pieView.bounds.size.width/2 - 17;
        rect.size.height = self.pieView.bounds.size.height/2 - 17;
    }else{
        rect.size.width = self.pieView.bounds.size.width/2 - 25;
        rect.size.height = self.pieView.bounds.size.height/2 - 25;
    }
    if (DeviceType >= 6) {
        rect.origin.x = self.pieView.bounds.origin.x+30;
        rect.origin.y = self.pieView.bounds.origin.y/2+60;
    }else{
        rect.origin.x = self.pieView.bounds.origin.x+30;
        rect.origin.y = self.pieView.bounds.origin.y/2+50;
    }
    _dounghnutView = [[RJDounghnutView alloc] initWithFrame:rect];
    [_dounghnutView setData:dataArray colors:colorsArray];
    [_dounghnutView buildEmpty];
    [_dounghnutView build:3];
    [self.pieContentView addSubview:_dounghnutView];
}

- (UILabel *)getCityBoxLabelWithTitle:(NSString *)title rect:(CGRect)rect
{
    UILabel * label = [[UILabel alloc] initWithFrame:rect];
    [label setText:title];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setTextColor:[UIColor blackColor]];
    [label setFont:[UIFont boldSystemFontOfSize:14.0f]];
    return label;
}

- (UILabel *)getLabelWithRect:(CGRect)rect title:(NSString *)title
{
    UILabel * label = [[UILabel alloc] initWithFrame:rect];
    [label setText:title];
    [label setTextColor:[UIColor blackColor]];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setFont:[UIFont boldSystemFontOfSize:14]];
    return label;
}

- (void)freeData
{

}




@end
